import javax.swing.text.html.*;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
 
/**
 *  Extract all "img" tags from an HTML document.
 */
public class imageDownloadFromUrl
{
  public static void main( String[] argv ) throws Exception
  {
    URL url = new URL( "http://www.indiaglitz.com/channels/tamil/gallery/Actress/4680.html" ); 
    HTMLEditorKit kit = new HTMLEditorKit(); 
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
    doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
    Reader HTMLReader = new InputStreamReader(url.openConnection().getInputStream()); 
    kit.read(HTMLReader, doc, 0); 
 
    //  Get an iterator for all HTML tags.
    ElementIterator it = new ElementIterator(doc); 
    
    
    Element elem; 
    String str,str1;
    
    while(true) // ever lasting...
    { 
       elem = it.next();
       
     
      if(elem == null) break; //..until the end is near.
      if( elem.getName().equals(  "img") )
      { 
        String s = (String) elem.getAttributes().getAttribute(HTML.Attribute.SRC);
        if( s != null ) 
          //System.out.println (s );
        
        if(s.endsWith("jpg")){
        	
        	//System.out.println (s );
        	
        	str=s.replace("thumbs/", "");
        	System.out.println (str );
        	//str1=str.lastIndexOf("/");
        	str1=str.substring(str.lastIndexOf("/")+1);
        	System.out.println (str1 );
        	
        	 URL url1 = new URL(str);
        	 InputStream in = new BufferedInputStream(url1.openStream());
        	 ByteArrayOutputStream out = new ByteArrayOutputStream();
        	 byte[] buf = new byte[1024];
        	 int n = 0;
        	 while (-1!=(n=in.read(buf)))
        	 {
        	    out.write(buf, 0, n);
        	 }
        	 out.close();
        	 in.close();
        	 byte[] response = out.toByteArray();
        	
        	
        	 FileOutputStream fos = new FileOutputStream("d://downloadimage//"+str1);
        	    fos.write(response);
        	    fos.close();
        }
      } 
    }
    System.exit(0);
  }
} 